/*
package com.demo.service.impl;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.demo.config.WxpayConfig;
import com.demo.dto.AlipayConfig;
import com.demo.dto.TradeVo;
import com.demo.mapper.ToolAlipayConfigMapper;
import com.demo.pojo.UnifiedorderResult;
import com.demo.service.AliPayService;
import com.demo.service.WeChatPayService;
import com.demo.util.HttpXmlUtils;
import com.demo.util.ParseXMLUtils;
import com.demo.util.WXSignUtils;
import javafx.beans.NamedArg;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;

*/
/**
 * @author leiliang
 * @version 1.0
 * @Description
 * @date 2021/1/6 11:43
 *//*

@Service
@Slf4j
public class WeChatPayServiceImpl implements WeChatPayService {


    @Autowired
    WxpayConfig wxpayconfig;

    @Override
    public Map<String, Object> goWeChatPay(Long orderId, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isEmpty(orderId)) {
            map.put("code", "-1");
            map.put("msg", "失败");
            return map;
        }
        //获取订单信息
        PayParameterForm payParameter = orderMapper.getPayParameter(orderId);
        double price = payParameter.getActualPrice();
        System.out.println("price:" + price);
        // 微信开放平台审核通过的应用APPID
        System.out.println("appid是：" + wxpayconfig.getAppid());
        System.out.println("mch_id是：" + wxpayconfig.getMch_id());
        String nonce_str = StringUtils.getRandomString(30);
        System.out.println("随机字符串是：" + nonce_str);
        int total_fee = (int) (price * 100);

        String total_price = null;// 订单总金额，单位为分，详见支付金额
        String spbill_create_ip = WXSignUtils.getRemortIP(request);// "127.0.0.1";
        System.out.println("spbill_create_ip====" + spbill_create_ip);
        String notify_url = wxpayconfig.getNotify_url();
        System.out.println("notify_url是：" + notify_url);
        String trade_type = "APP";

        // 参数：开始生成签名
        SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
        parameters.put("appid", wxpayconfig.getAppid());
        parameters.put("body", payParameter.getTitle());
        parameters.put("mch_id", wxpayconfig.getMch_id());
        parameters.put("nonce_str", nonce_str);
        parameters.put("notify_url", notify_url);
        parameters.put("out_trade_no", String.valueOf(payParameter.getOrderId()));
*/
/*
        parameters.put("total_fee", total_fee);
*//*

        parameters.put("spbill_create_ip", spbill_create_ip);
        parameters.put("total_fee", 1);
        parameters.put("trade_type", trade_type);
        String sign = WXSignUtils.createSign("UTF-8", parameters);
        System.out.println("签名是：" + sign);
        Unifiedorder unifiedorder = new Unifiedorder();
        unifiedorder.setAppid(wxpayconfig.getAppid());
        unifiedorder.setBody(payParameter.getTitle());
        unifiedorder.setMch_id(wxpayconfig.getMch_id());
        unifiedorder.setNonce_str(nonce_str);
        unifiedorder.setNotify_url(notify_url);
        unifiedorder.setOut_trade_no(String.valueOf(payParameter.getOrderId()));
        unifiedorder.setSpbill_create_ip(spbill_create_ip);
        unifiedorder.setTotal_fee(1);
        unifiedorder.setTrade_type(trade_type);
        unifiedorder.setSign(sign);

        // 构造xml参数
        String xmlInfo = HttpXmlUtils.xmlInfo(unifiedorder);
        System.out.println("xmlInfo:" + xmlInfo);

        String wxUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        String method = "POST";
        String weixinPost = HttpXmlUtils.httpsRequest(wxUrl, method, xmlInfo).toString();// 请求微信
        System.out.println("weixinPost:" + weixinPost);
        UnifiedorderResult unifiedorderResult = ParseXMLUtils.jdomParseXml(weixinPost);// 解析微信的反馈
        if (unifiedorderResult != null) {
            if ("SUCCESS".equals(unifiedorderResult.getReturn_code())) {
                if ("INVALID_REQUEST".equals(unifiedorderResult.getErr_code())) {
                    map.put("code", "-1");
                    map.put("msg", "参数错误");
                    return map;
                }
                // 开始拼接App调起微信的参数
                SortedMap<Object, Object> wxAppparameters = new TreeMap<Object, Object>();
                wxAppparameters.put("appid", unifiedorderResult.getAppid());
                wxAppparameters.put("partnerid", unifiedorderResult.getMch_id());
                wxAppparameters.put("prepayid", unifiedorderResult.getPrepay_id());
                wxAppparameters.put("package", wxpayconfig.getWx_package());
                wxAppparameters.put("noncestr", nonce_str);
                wxAppparameters.put("timestamp", String.valueOf(new Date().getTime()).substring(0, 10));
                wxAppparameters.put("sign", WXSignUtils.createSign("UTF-8", wxAppparameters));
                map.put("code", "0");
                map.put("msg", "成功");
                map.put("data", wxAppparameters);
                return map;
            } else {
                System.out.println("错误原因为：" + unifiedorderResult.getReturn_msg());
                map.put("code", "-1");
                map.put("msg", unifiedorderResult.getReturn_msg());
                return map;
            }
        } else {
            System.out.println("服务端请求微信的返回值异常。");
            map.put("code", "-1");
            map.put("msg", "服务端请求微信的返回值异常。");
            return map;
        }
    }


    @Override
    public String weChatNotify(HttpServletRequest request) throws Exception {
        Map<String, String> map = new HashMap<>();
        System.out.println("----------------微信回调开始啦----------------------");
        // 读取参数
        InputStream inputStream;
        StringBuffer sb = new StringBuffer();
        inputStream = request.getInputStream();
        String s;
        BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
        while ((s = in.readLine()) != null) {
            sb.append(s);
        }
        in.close();
        inputStream.close();
        // 解析xml成map
        Map<String, String> m = new HashMap<String, String>();
        m = WXSignUtils.doXMLParse(sb.toString());
        // 过滤空 设置 TreeMap
        SortedMap<Object, Object> packageParams = new TreeMap<Object, Object>();
        Iterator<String> it = m.keySet().iterator();
        while (it.hasNext()) {
            String parameter = it.next();
            String parameterValue = m.get(parameter);

            String v = "";
            if (null != parameterValue) {
                v = parameterValue.trim();
            }
            System.out.println("p:" + parameter + ",v:" + v);
            packageParams.put(parameter, v);
        }
        // 微信支付的API密钥
        String key = wxpayconfig.getKey();
        if (!isTenpaySign("UTF-8", packageParams, key)) {
            map.put("return_code", "FAIL");
            map.put("return_msg", "return_code不正确");
            return StringUtil.GetMapToXML(map);
        }
        //返回状态存入redis中
        if (m.get("return_code").equals("SUCCESS")) {
            RedisUtil.set("wx" + m.get("out_trade_no"), m.get("result_code"), 300);
        }
        if (isTenpaySign("UTF-8", packageParams, key)) {
            // 验证通过
            if ("SUCCESS".equals((String) packageParams.get("result_code"))) {
                String out_trade_no = (String) packageParams.get("out_trade_no");
                */
/* 订单不为空 *//*

                if (!Util.isEmpty(out_trade_no)) {
                    //支付成功后的业务处理
                    OrderEntity order = orderMapper.getOrderInfo(Long.valueOf(out_trade_no));
                    if (!Util.isEmpty(order)) {
                        order.setStatus(CalculatStaticConstant.CHECK_ONE);
                        order.setCompleteTime(DateUtil.currentDate());
                        orderMapper.updateOrder(order);
                        System.out.println("----------------修改订单状态----------------------");
                    }
                    */
/* 添加支付信息 *//*

                    OrderPayEntity orderPay = new OrderPayEntity();
                    orderPay.setId(Long.valueOf(IdUtils.getPrimaryKey()));
                    orderPay.setOrderId(order.getId());
                    orderPay.setUserId(order.getUserId());
                    orderPay.setPayPrice(order.getActualPrice());
                    orderPay.setPayType(PayTypeEnum.WE_CHAT_PAY.intKey());
                    orderPay.setStatus(CalculatStaticConstant.CHECK_ONE);
                    orderPay.setPayTime(DateUtil.currentDate());
                    orderMapper.saveOrderPay(orderPay);
                    System.out.println("----------------添加支付信息----------------------");
                    map.put("return_code", "SUCCESS");
                    map.put("return_msg", "OK");
                    return com.demo.util.StringUtil.GetMapToXML(map);
                }
            }
        } else {
            System.out.println("支付失败");
            map.put("return_code", "error");
            map.put("return_msg", "支付失败");
            return com.demo.util.StringUtil.GetMapToXML(map);
        }
        System.out.println("支付失败");
        System.out.println("支付失败");
        map.put("return_code", "error");
        map.put("return_msg", "支付失败");
        return com.demo.utilStringUtil.GetMapToXML(map);
    }


    */
/**
     * @Function: 是否签名正确, 规则是:按参数名称a-z排序,遇到空值的参数不参加签名。
     * @author: YangXueFeng
     * @Date: 2019/6/17 17:10
     *//*

    @SuppressWarnings("rawtypes")
    public static boolean isTenpaySign(String characterEncoding, SortedMap<Object, Object> packageParams,
                                       String API_KEY) {
        StringBuffer sb = new StringBuffer();
        Set es = packageParams.entrySet();
        Iterator it = es.iterator();
        while (it.hasNext()) {
            Map.Entry entry = (Map.Entry) it.next();
            String k = (String) entry.getKey();
            String v = (String) entry.getValue();
            if (!"sign".equals(k) && null != v && !"".equals(v)) {
                sb.append(k + "=" + v + "&");
            }
        }
        sb.append("key=" + API_KEY);
        // 算出摘要
        String mysign = DigestUtils.md5(sb.toString()).toString().toUpperCase();
        String tenpaySign = ((String) packageParams.get("sign")).toLowerCase();
        return tenpaySign.equals(mysign);
    }

}
*/
